package ch2.sq;

public class Exam2_6 {
    public static SqListClass<Integer> Merge2(SqListClass<Integer> A, SqListClass<Integer> B) { //例2.6的算法
        SqListClass<Integer> C = new SqListClass<>();
        int i = 0, j = 0;                       //i用于遍历A的下标,j用于遍历B的下标
        while (i < A.size() && j < B.size()) {  //两个表均没有遍历完毕
            if (A.GetElem(i) < B.GetElem(j)) {
                C.Add(A.GetElem(i));    //将较小的A中元素添加到C中
                i++;
            } else {
                C.Add(B.GetElem(j));    //将较小的B中元素添加到C中
                j++;
            }
        }
        // 当A和B的长度不等的时候
        while (i < A.size()) {  //若A没有遍历完毕
            C.Add(A.GetElem(i));
            i++;
        }
        while (j < B.size()) {  //若B没有遍历完毕
            C.Add(B.GetElem(j));
            j++;
        }
        return C;
    }

    public static void main(String[] args) {
        // 1. 创建递增的顺序表a
        Integer[] a = {1, 3, 5, 7};
        SqListClass<Integer> A = new SqListClass<>();
        A.CreateList(a);
        System.out.println("A: " + A);

        // 2. 创建增的顺序表b
        Integer[] b = {1, 2, 5, 7, 9, 10, 20};
        SqListClass<Integer> B = new SqListClass<>();
        B.CreateList(b);
        System.out.println("B: " + B);

        // 3. 执行二路归并算法代码
        System.out.println("二路归并");
        SqListClass<Integer> C = Exam2_6.Merge2(A, B);
        System.out.println("C: " + C);
    }
} 
